﻿using Aspose.Cells.Drawing;
using Dapper;
using Estsh.Core.Base;
using Estsh.Core.Model.Extend.EnumUtil;
using Estsh.Core.Model.Extend.Models;
using Estsh.Core.Model.Result;
using Estsh.Core.Models;
using Estsh.Core.Services;
using Estsh.Core.Util;
using Estsh.Core.Wms.IRepositories;
using Estsh.Core.Wms.IServices;
using Microsoft.Extensions.Primitives;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Estsh.Core.Wms.Services
{
	public class ProductionReportingService : BaseService<BaseEntity>, IProductionReportingService
	{
		private readonly IProductionReportingRepository repository;
		public ProductionReportingService(IProductionReportingRepository _repository) : base(_repository)
		{
			repository = _repository;
		}

		public List<Syspdline_Extend> GetSysPdlineList(string userId)
		{
			return repository.GetSysPdlineList(userId);
		}
		/// <summary>
		/// 根据工作中心来查询工单
		/// </summary>
		/// <param name="pdlineId"></param>
		/// <returns></returns>
		public List<MesWorkOrder_Extend> GetWorkOrderByPdlineId(string pdlineId)
		{
			return repository.GetWorkOrderByPdlineId(pdlineId);
		}

		/// <summary>
		///根据工单、产线来查询 产线和人员的关系
		/// </summary>
		/// <returns></returns>
		public SetObjectDetail GetEmPdlineByOrderNoAndPdline(string pdlineId, string orderNo)
		{

			SetObjectDetail rfInfo = new SetObjectDetail();//返回消息
			List<SysEmpPdline> sysEmpPdlines = repository.GetEmPdlineByOrderNoAndPdline(pdlineId, orderNo);
			rfInfo.type = "PASS";
			rfInfo.message = "成功";
			rfInfo.wmsObjectDetail = sysEmpPdlines.ConvertAll(s => (object)s);
			return rfInfo;
		}
		/// <summary>
		/// 根据工作中心、工单号来查询工单
		/// </summary>
		/// <returns></returns>
		public SetObjectDetail GetWorkOrderByPdlineIdAndOrderNo(string pdlineId, string orderNo)
		{

			SetObjectDetail rfInfo = new SetObjectDetail();//返回消息
			List<MesWorkOrder_Extend> sysEmpPdlines = repository.GetWorkOrderByPdlineIdAndOrderNo(pdlineId, orderNo);
			rfInfo.type = "PASS";
			rfInfo.message = "成功";
			rfInfo.wmsObjectDetail = sysEmpPdlines.ConvertAll(s => (object)s);
			return rfInfo;
		}


		/// <summary>
		/// 保存人员与产线的关系
		/// </summary>
		/// <returns></returns>
		public SetObjectDetail SaveEmpByPdline(string empNo, string pdlineId, string orderNo, string loginId)
		{


			SetObjectDetail rfInfo = new SetObjectDetail();
			int isSuccess = repository.SaveEmpByPdline(empNo, pdlineId, orderNo, loginId); ;

			if (isSuccess == 1)
			{
				rfInfo.type = "PASS";
				rfInfo.message = "操作成功！";
				//rfInfo.wmsObjectDetail = wmsMoves.ConvertAll(s => (object)s);
			}
			else if (isSuccess == -1)
			{
				rfInfo.type = "NO";
				rfInfo.message = "没有查询到此人员信息";
			}
			else if (isSuccess == -2)
			{
				rfInfo.type = "NO";
				rfInfo.message = "【"+ empNo + "】员工不在此工作中心内，请检查！";
			}
			else
			{
				rfInfo.type = "FAIL";
				rfInfo.message = "操作失败！";
			}
			return rfInfo;

		}

		/// <summary>
		/// 查询零件号是否是半成品还是成品
		/// </summary>
		/// <param name="partId"></param>
		/// <returns></returns>
		public SetObjectDetail GetworkOrderByPartType(string partId)
		{

			SetObjectDetail rfInfo = new SetObjectDetail();//返回消息
			List<SysPart_Extend> partinfos = repository.GetworkOrderByPartType(partId);
			if (partinfos.Count > 0)
			{
				rfInfo.type = "PASS";
				rfInfo.message = "成功";
				rfInfo.wmsObjectDetail = partinfos.ConvertAll(s => (object)s);
			}
			else
			{
				rfInfo.type = "NO";
				rfInfo.message = "失败没有此零件";
			}


			return rfInfo;
		}

		/// <summary>
		/// 保据扫描的周转箱，判断它是否存在
		/// </summary>
		/// <param name="cartonNo"></param>
		/// <returns></returns>
		public SetObjectDetail GetSysStockByCartonNo(string cartonNo, string partNo)
		{

			SetObjectDetail rfInfo = new SetObjectDetail();//返回消息
			List<SysStock_Extend> sysStockInfos = repository.GetSysStockByCartonNo(cartonNo);
			if (sysStockInfos.Count > 0)
			{
				//查询周转箱关系表中是否存在关联的箱条码
				List<WmsRackPackage_Extend> wmsRackPackages = repository.GetWmsRackByCartonNo(cartonNo);
				if (wmsRackPackages.Count() > 0)
				{ 
					List<WmsRackPackage_Extend> wmsRacks= wmsRackPackages.Where(s=>s.PartNo== partNo).ToList();
					if(wmsRacks.Count()!= wmsRackPackages.Count())
					{
						rfInfo.type = "NO";
						rfInfo.message = "【" + cartonNo + "】周转箱上有不同的零件，请检查！";
						return rfInfo;
					}
				}


				rfInfo.type = "PASS";
				rfInfo.message = "【" + cartonNo + "】扫描成功！";
				rfInfo.wmsObjectDetail = sysStockInfos.ConvertAll(s => (object)s);
				return rfInfo;
			}
			else
			{
				rfInfo.type = "NO";
				rfInfo.message = "【" + cartonNo + "】没有此周装箱，请重新扫描！";
				return rfInfo;
			}


		
		}

		/// <summary>
		/// 验证模具号和生成报工数据
		/// </summary>
		/// <param name="modelNo">模具号</param>
		/// <param name="cartonNo">周转箱</param>
		/// <param name="Num">下线数</param>
		/// <param name="pdlineId">产线</param>
		/// <param name="orderNo">工单号</param>
		/// <param name="loginId">操作人</param>
		/// <param name="factoryId">工厂</param>
		/// <returns></returns>
		public SetObjectDetail SaveModelNoByWorkReport(string modelNo, string cartonNo, string Num, string pdlineId, string orderNo, string loginId, string factoryId)
		{

			SetObjectDetail rfInfo = new SetObjectDetail();//返回消息

			//判断是否存在模具号

			List<SysEquipment> sysEquipmentInfo = repository.GetEquipmentByModelNo(modelNo);//查询模具号
			if (sysEquipmentInfo.Count > 0)
			{
				if (sysEquipmentInfo[0].EquipmentStatus != 1)
				{
					rfInfo.type = "NO";
					rfInfo.message = "【" + modelNo + "】模具号状态不正常，请检查！";
					return rfInfo;
				}
				if (sysEquipmentInfo[0].Maximum< (sysEquipmentInfo[0].EquipmentNum+int.Parse(Num)))
				{
					rfInfo.type = "NO";
					rfInfo.message = "下线数量，超过模具预警最大值，请检查！";
					return rfInfo;
				}
				#region 查询数据
				//查询产线
				List<Syspdline_Extend> syspdlineInfos = repository.GetPdlineByPdlineId(pdlineId);
				//查询工单
				List<MesWorkOrder_Extend> mesWorkOrderInfos = repository.GetWorkOrderByOrderNoByPdlineId(orderNo, pdlineId);
				//人员和产线的关系信息
				List<SysEmpPdline> sysEmpPdlineInfos = repository.GetEmPdlineByOrderNoAndPdline(pdlineId, orderNo);
				
				//零件清单
				List<SysPart_Extend> sysPartInfos = new List<SysPart_Extend>();
				sysPartInfos = repository.GetSysPartByPartId(mesWorkOrderInfos[0].PartId.ToString());


				#endregion

				List<MesWorkOrder_Extend> workOrderInfos = repository.GetWorkOrderByPdlineIdAndOrderNo(pdlineId, orderNo);
				if (workOrderInfos[0].OrderStatus == (int)EnumUtil_Extend.MesWorkOrderStatus.FIRING)//启动中
				{
					List<string> sqlStrings = new List<string>();
					StringBuilder SqlStringBuilder = new StringBuilder(1024);
					//修改工单下线数量
					#region workoOrder表
					SqlStringBuilder = new StringBuilder(1024);
					SqlStringBuilder.Append("  UPDATE dbo.mes_work_order SET inserting_coil_qty=(ISNULL(inserting_coil_qty, 0)+" + decimal.Parse(Num) + ") , ");
					if (mesWorkOrderInfos[0].Qty <= ((mesWorkOrderInfos[0].InsertingCoilQty == null ? 0 : mesWorkOrderInfos[0].InsertingCoilQty) + decimal.Parse(Num)))//当需求数量等于下线数量时，将工单状态改为完成50
					{
						SqlStringBuilder.Append(" order_status= " + (int)EnumUtil_Extend.MesWorkOrderStatus.COMPLETE + ", ");
					}
					SqlStringBuilder.Append("  update_userid=" + loginId + ",update_time='" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "' ");
					SqlStringBuilder.Append("  WHERE order_no='" + orderNo + "' AND ruid='" + mesWorkOrderInfos[0].Ruid + "'; ");
					sqlStrings.Add(SqlStringBuilder.ToString());
					#endregion

					#region 修改模具数量sys_equipment
					SqlStringBuilder = new StringBuilder(1024);
					SqlStringBuilder.Append("  UPDATE dbo.sys_equipment SET equipment_num=(ISNULL(equipment_num, 0)+" + decimal.Parse(Num) + ") , ");
					SqlStringBuilder.Append("  update_userid=" + loginId + ",update_time='" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "' ");
					SqlStringBuilder.Append("  WHERE equipment_no='" + modelNo + "'; ");
					sqlStrings.Add(SqlStringBuilder.ToString());

					#endregion


					#region 报工数mes_out_pdline

					//报工写入mes_out_pdline 根据明细写入
					int upRounding = Convert.ToInt32(Math.Ceiling(decimal.Parse(Num) / Convert.ToInt64(sysPartInfos[0].MinPackQty)));//圆整

					////生成批次
					string lot_no = DateTime.Now.ToString("yyyyMMdd");


					//判断周转箱是否存在WmsRack表
					if (sysPartInfos[0].PartType.Equals("0"))
					{
						List<WmsRack_Extend> wmsRackInfos = repository.GetWmsRack(cartonNo);
						if (wmsRackInfos.Count() == 0)
						{
							SqlStringBuilder = new StringBuilder(1024);
							SqlStringBuilder.Append(" INSERT INTO dbo.wms_rack ( ");
							SqlStringBuilder.Append(" [rack_no], [rack_type], [packageType],    ");
							SqlStringBuilder.Append(" [factory_id], [factory_code],    ");
							SqlStringBuilder.Append(" [enabled], [create_userid], [create_time], [guid])   ");
							SqlStringBuilder.Append(" VALUES(   ");
							SqlStringBuilder.Append(" '" + cartonNo + "', '10','1',  ");//周转箱、类型：1
							SqlStringBuilder.Append(" '" + mesWorkOrderInfos[0].FactoryId + "','" + mesWorkOrderInfos[0].FactoryCode + "',  ");//工厂id、工厂代码
							SqlStringBuilder.Append(" 'Y', " + loginId + ",'" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "' ,'" + Guid.NewGuid() + "' ) ;  ");//创建人、创建时间、
							sqlStrings.Add(SqlStringBuilder.ToString());

						}

					}

					for (int i = 0; i < upRounding; i++)
					{
						//生成箱条码
						string BoxCode = repository.GetOrderNo("NbSnNumber", "SN");
						BoxCode = BoxCode.Trim().ToString();//去掉空格

						//判断是否报工
						if (syspdlineInfos[0].IsWorkReport)//1为报工、0为不报工
						{

							SqlStringBuilder = new StringBuilder(1024);
							SqlStringBuilder.Append("INSERT INTO dbo.mes_out_pdline (work_order_no,pdline_id,pdline_code,part_id,part_no,part_desc ");
							SqlStringBuilder.Append("           ,serial_number,qty,lot_no,product_type,report_status,erp_status,err_msg ");
							SqlStringBuilder.Append("           ,factory_id,factory_code,enabled,create_userid,create_time,Guid) ");
							SqlStringBuilder.Append("VALUES( ");

							SqlStringBuilder.Append(" '" + orderNo + "' ,'" + syspdlineInfos[0].PdlineId + "','" + syspdlineInfos[0].PdlineCode + "',");//工单号、产线、产线代码
							if (sysPartInfos.Count > 0)
							{
								SqlStringBuilder.Append(" '" + sysPartInfos[0].PartId + "' ,'" + sysPartInfos[0].PartNo + "', '"+ sysPartInfos[0].PartSpec+ "' ,");//零件号ID、//零件号//零件号简码
							}
							else
							{
								SqlStringBuilder.Append(" '" + 0 + "' ,'" + null + "','"+null+"',");//零件号ID、//零件号//零件号简码
							}
							SqlStringBuilder.Append(" '" + BoxCode + "' ,");//产品条码
							SqlStringBuilder.Append("  '" + sysPartInfos[0].MinPackQty + "','" + lot_no + "','" + 0 + "',10,10,'" + null + "', ");// 数量 \生产批次 \生产类型\生产版本
							SqlStringBuilder.Append("  '" + mesWorkOrderInfos[0].FactoryId + "','" + mesWorkOrderInfos[0].FactoryCode + "','Y','" + loginId + "','" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "','" + Guid.NewGuid() + "');");
							sqlStrings.Add(SqlStringBuilder.ToString());
						}

						#region 记录扫描的工单数据g_workorder_sn

						SqlStringBuilder = new StringBuilder(1024);
						SqlStringBuilder.Append(" INSERT INTO dbo.g_workorder_sn ( ");
						SqlStringBuilder.Append(" [seq], [workorder],    ");
						SqlStringBuilder.Append(" [wo_detail_id],[serial_number], [printed], [reprint_count],    ");
						SqlStringBuilder.Append(" [part_id], [rack_no], [carton_no], [qty],[lot_no],     ");
						SqlStringBuilder.Append(" [inserting_coil_qty], [equipment_no], [pdline_id], [equipment_num], ");
						SqlStringBuilder.Append(" [enabled], [create_userid], [create_time], [guid])   ");
						SqlStringBuilder.Append(" VALUES(   ");
						SqlStringBuilder.Append(" " + 0 + ", '" + orderNo + "',  ");//序号、工单号
						SqlStringBuilder.Append(" " + 0 + ", '" + BoxCode + "', '" + 0 + "','" + 0 + "',  ");// 、序号、箱条码、打印、重打印次数
						SqlStringBuilder.Append(" '" + mesWorkOrderInfos[0].PartId + "', '" + cartonNo + "', '" + BoxCode + "','" + sysPartInfos[0].MinPackQty + "','" + lot_no + "' , ");//零件号、周转箱、箱条码、数量、批次
						SqlStringBuilder.Append(" '" + Num + "', '" + modelNo + "', '" + pdlineId + "','" + Num + "',  ");//下线数量、设备号、产线id、设备次数
						SqlStringBuilder.Append(" 'Y', " + loginId + ",'" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "' ,' " + Guid.NewGuid() + "' ) ;  ");//创建人、创建时间、

						sqlStrings.Add(SqlStringBuilder.ToString());


						#endregion


						#region 要不是成品，将箱条码和周转箱绑定 wms_rack_package表
						if (sysPartInfos[0].PartType.Equals("0"))//零件表中 1:总成、 0：原材料、 2:配置
						{
							SqlStringBuilder = new StringBuilder(1024);
							SqlStringBuilder.Append(" INSERT INTO dbo.wms_rack_package ( ");
							SqlStringBuilder.Append(" [rack_no], [corton_no], [packageType],    ");
							SqlStringBuilder.Append(" [part_no], [rack_num], [factory_id], [factory_code],    ");
							SqlStringBuilder.Append(" [enabled], [create_userid], [create_time], [guid])   ");
							SqlStringBuilder.Append(" VALUES(   ");
							SqlStringBuilder.Append(" '" + cartonNo + "', '" + BoxCode + "','1',  ");//周转箱、箱条码、类型：1
							SqlStringBuilder.Append(" '" + sysPartInfos[0].PartNo + "', '" + Convert.ToInt32(sysPartInfos[0].MinPackQty) + "','" + mesWorkOrderInfos[0].FactoryId + "','" + mesWorkOrderInfos[0].FactoryCode + "',  ");//零件号、数量、工厂id、工厂代码
							SqlStringBuilder.Append(" 'Y', " + loginId + ",'" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "' ,'" + Guid.NewGuid() + "' ) ;  ");//创建人、创建时间、
							sqlStrings.Add(SqlStringBuilder.ToString());

						}

						#endregion


					}

					#endregion


					#region 记录工单与员工的关系Sys_emp_work_ordey
					foreach (var info in sysEmpPdlineInfos)
					{
						SqlStringBuilder = new StringBuilder(1024);
						SqlStringBuilder.Append(" INSERT INTO dbo.sys_emp_work_ordey (  ");
						SqlStringBuilder.Append(" [emp_id], [emp_no], [order_no],   ");
						SqlStringBuilder.Append(" [part_no], [work_report_num], [rack_no],   ");
						SqlStringBuilder.Append(" [enabled], [create_userid], [create_time], [guid])   ");
						SqlStringBuilder.Append(" VALUES(   ");
						SqlStringBuilder.Append(" " + info.EmpId + ", '" + info.EmpNo + "', N'" + orderNo + "', ");//员工id、工号、工单号
						SqlStringBuilder.Append(" '" + mesWorkOrderInfos[0].PartNo + "', '" + Num + "', '" + cartonNo + "',  ");//零件号、下线数量、周转箱 
						SqlStringBuilder.Append(" 'Y', " + loginId + ",'" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "' ,'" + Guid.NewGuid() + "' ) ;  ");//创建人、创建时间、

						sqlStrings.Add(SqlStringBuilder.ToString());
					}

					#endregion


					int n = repository.SQLTransaction(sqlStrings, null);
					if (n > 0)
					{
						rfInfo.type = "PASS";
						List<MesWorkOrder_Extend> workOrderInfo = repository.GetWorkOrderByPdlineIdAndOrderNo(pdlineId, orderNo);
						if (workOrderInfo[0].OrderStatus == (int)EnumUtil_Extend.MesWorkOrderStatus.COMPLETE)//已完成
						{
							//生成质检单
							int isQc = CreatrQc(workOrderInfo[0], loginId, sysPartInfos[0]);
							if (isQc > 0)
							{
								workOrderInfo = new List<MesWorkOrder_Extend>();
								rfInfo.message = "【" + orderNo + "】工单已生成质检单！";
								rfInfo.wmsObjectDetail = workOrderInfo.ConvertAll(s => (object)s);
								return rfInfo;
							}
							else
							{
								rfInfo.message = "【" + orderNo + "】工单生产质检单失败！";
								rfInfo.wmsObjectDetail = workOrderInfo.ConvertAll(s => (object)s);
								return rfInfo;
							}
						}
						else
						{
							rfInfo.message = "报工完成！";
							rfInfo.wmsObjectDetail = workOrderInfo.ConvertAll(s => (object)s);
							return rfInfo;
						}
					}


				}


			}
			else
			{
				rfInfo.type = "NO";
				rfInfo.message = "【" + modelNo + "】模具号不存在，请重新扫描！";
				return rfInfo;
			}

			return rfInfo;
		}


		public int CreatrQc(MesWorkOrder_Extend workOrderInfo, string loginId,SysPart_Extend partInfo)
		{

			#region 要是下线数量等于需求数 工单完成，生成质检单

			//免检清单
			List<SysExemptionList> exemptionList = repository.Getqc();


			List<GWorkOrderSn_Extend> gWorkOrderSnInfos = repository.GetGWrokOrderSn(workOrderInfo.OrderNo.ToString()); //查询 workOrdersn表

			List<string> sqlStrings = new List<string>();
			StringBuilder SqlStringBuilder = new StringBuilder(1024);
			if (gWorkOrderSnInfos.Count > 0)
			{
				//生成质检单号
				string _QcOrder = repository.GetOrderNo("QCList", "QC");
				string QcOrder = _QcOrder.Trim().ToString();//去掉空格

				int t = 0;
				for (int i = 0; i < gWorkOrderSnInfos.Count(); i++)
				{
					//验证是否是免检
					//List<SysExemptionList> exemption = exemptionList.Where(s => s.PartId == gWorkOrderSnInfos[0].PartId).ToList();
					//if (exemption.Count() > 0)
					//{
					//	continue;
					//}
					t++;

					List<SysPart_Extend> sysPartInfos = new List<SysPart_Extend>();
					sysPartInfos = repository.GetSysPartByPartId(gWorkOrderSnInfos[0].PartId.ToString());//查询零件信息

					StringBuilder insertString2 = new StringBuilder(1024);//质检明细表
					insertString2.Append("INSERT INTO [dbo].[wms_qc_detail] ([order_no] ,[part_id],[part_no] ,[part_spec]  ,[carton_no] ,[lot_no] ,[qty] ,[nc_qty]");
					insertString2.Append(" ,[item_status]  ,[remark] ,[factory_id] ,[factory_code] ,[enabled]  ,[create_userid]  ,[create_time]   ,[guid])   VALUES ( ");
					insertString2.Append(" '" + QcOrder + "','" + sysPartInfos[0].PartId + "','" + sysPartInfos[0].PartNo + "','" + sysPartInfos[0].PartSpec + "','" + gWorkOrderSnInfos[i].CartonNo + "','" + gWorkOrderSnInfos[i].lotNo + "','" +Convert.ToInt32(gWorkOrderSnInfos[i].Qty) + "','" + 0 + "',   ");
					insertString2.Append(" '10','" + null + "','" + workOrderInfo.FactoryId + "','" + workOrderInfo.FactoryCode + "','Y','" + loginId + "','" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "','"+ Guid.NewGuid() + "'  );");
					sqlStrings.Add(insertString2.ToString());

				}
				if (t > 0)
				{//质检主表
					#region 生成质检主表

					StringBuilder insertString = new StringBuilder(1024);
					insertString.Append(" INSERT INTO [dbo].[wms_qc_header] ([order_no],[vendor_id] ,[vendor_code]  ,[order_status] ,[ref_order_no] ,[remark] ,[factory_id] ,[factory_code] ,[enabled] ,[create_userid],[create_time],[guid]) VALUES ( ");
					insertString.Append("  '" + QcOrder + "', '" + null + "'  ,'" + null + "'  ,'10'    ,'" + workOrderInfo.OrderNo + "',");
					insertString.Append("  '" + null + "', '" + workOrderInfo.FactoryId + "'  ,'" + workOrderInfo.FactoryCode + "'  ,'Y',");
					insertString.Append("  '" + loginId + "', '" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "'  ,'" + Guid.NewGuid() + "'  ); ");
					sqlStrings.Add(insertString.ToString());

					#endregion

					#region 接口数据 WMS_TOQC
					StringBuilder insertSAPQC = new StringBuilder(1024);
					insertSAPQC.Append(" INSERT INTO [dbo].[WMS_TOQC] ([BSTYP]  ,[EVRTN] ,[EVRTNLINE]");
					insertSAPQC.Append("                              ,[ISQCCOM] ,[ETENR] ,[QCNUM] ,[PART_NO]  ,[ZDEV]  ,[NZDEV]");
					insertSAPQC.Append("                              ,[UNIT]    ,[ZPOST] ,[ZTIME] ,[WERKS]    ,[LIFNR] ,[QCOPERATOR]");
					insertSAPQC.Append("                              ,[LOTON]    ,[SID] ,[ACTTIM] ,[ACTUSR]    ,[STAFLG])  VALUES( ");
					insertSAPQC.Append("                              '1'    ,'" + workOrderInfo.OrderNo + "' ,'"+0+"', "); //单据类型/关联单号/关联单据中该物料所在行号 
					insertSAPQC.Append("                              'Y'    ,'" + QcOrder + "' , '" + workOrderInfo.Qty+ "','"+ workOrderInfo.PartNo+ "','"+0+"','"+0+"', "); //质检通知标识     Y:需要质检/单号 qc单号/应质检数量/零件号/合格数量
					insertSAPQC.Append("                              '"+ partInfo.Unit + "'    ,'" + DateTime.Now.ToString("yyyy-MM-dd") + "' , '" + DateTime.Now.ToString("HH:mm:ss") + "','" + workOrderInfo.FactoryId+ "','" + null + "','" + loginId + "', ");//计量单位/日期/时间/工厂id/供应商代码//质检操作人
					insertSAPQC.Append("                              '" + gWorkOrderSnInfos[0].lotNo + "'    ,'" + Guid.NewGuid().ToString().Replace("-", "") + "' , '" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "','" + loginId + "','" + 0 + "' );");//批次/标识符/处理日期/处理人/状态标志   待确认

					sqlStrings.Add(insertSAPQC.ToString());

					#endregion


				}

				return repository.SQLTransaction(sqlStrings, null);


			}
			else
			{ 
				return 0;
			}



			#endregion

		}


	}
}
